-module(live_qrec_service).
-include("rec_dec.hrl").
-include_lib("stdlib/include/qlc.hrl").
-export([start/0,addchange/1,get/0,get/1,del/1,restore/0,clear/0]).
-define(TAB    , live_q_rec).

start() ->
	case db_mgr:check_table(?TAB) of
	no_found ->
		db_mgr:create_table(?TAB,record_info(fields,cust_in_q_rec),[{record_name,cust_in_q_rec}]);
	_ ->
		ok
	end.


addchange(Rec) when is_record(Rec,cust_in_q_rec) ->
	db_mgr:write(?TAB,Rec).
	

get() -> %% get all cust info
	db_mgr:get(?TAB).
	
	
get({queue,Queue_Id}) -> %% get the cust queue rec based on queue id
	Qry = qlc:q([X||X <- mnesia:table(?TAB),X#cust_in_q_rec.q_id == Queue_Id]),
	db_mgr:do_transaction(fun() -> qlc:e(Qry) end).

%%
%% obsolete	
restore() ->
	lists:foreach(
		fun(C) ->
			gen_server:call(biz_vendor,{forward_queue_msg,{C#cust_in_q_rec.vendor_id,C#cust_in_q_rec.q_id},{restore,C}})
		end,
		live_qrec_service:get()
	).

del({cust_rec,Id}) ->
	io:format("live_qrec delete for ~p",[Id]),
	db_mgr:del(?TAB,Id).


	
%% drop and create a new table
clear() ->	
	live_qrec_service:start().
    